const app = require('./config/express.js');
const config = require('./config/env/config_development.json');
const http = require('http');
const winston = require('winston');
const mongoose = require('mongoose');

///连接mongodb数据库
if(config.mongodb.debug){
	mongoose.set('debug', true);
}
var db = mongoose.connect(config.mongodb.uri, config.mongodb.options,function(err){
	if(err){
		console.error('Could not connect to MongoDB!');
		console.error(err);
		process.exit(-1);
	}else{
		console.log(`Connected to MongoDB ---> ${config.mongodb.uri}`);
	}
});


let server = http.createServer(app);

server.listen(config.NJ_DOM_CEAT.port);

// exception handling
process.on('uncaughtException', function(err) {
	winston.error('Caught exception:', err);
});

// Expose app
exports = module.exports = app;

// Logging initialization
winston.info('------------------------------------------------');
winston.info(config.NJ_DOM_CEAT.title, 'application started');
winston.info('Environment:\t\t\t', process.env.NODE_ENV);
winston.info('Port:\t\t\t\t', config.NJ_DOM_CEAT.port);
if (process.env.NODE_ENV === 'secure') {
	winston.info('HTTPs:\t\t\t\ton');
}
winston.info('------------------------------------------------');
